package com.ttit.helloworld.view;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;


import com.ttit.helloworld.R;


public class MySeekBar extends LinearLayout {

    private int testnum = 0;
    private int mMax = 100;
    private ImageView pro_img;
    private ImageView ivthumb;
    private int mProgress = -1;
    private int mProgress_first = 0;
    private int myseekbar_w = 0;
    private int myseekbar_h = 0;

    private int thumb_width = 33;


    private Bitmap bp_fillbar = null;
    private Bitmap bp_thumb = null;
    int proImg_w = 0;
    private boolean hasMeasured = false;

    private OnMyseekBarChangeListener mOnMyseekBarChangeListener;
    private String TAG = MySeekBar.class.getSimpleName();

    public MySeekBar(Context context) {
        super(context);
    }

    public MySeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.myprobar, this, true);
        pro_img = (ImageView) findViewById(R.id.pro_img);
        ivthumb = (ImageView) findViewById(R.id.thumb);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MySeekBar);
        mMax = a.getInt(R.styleable.MySeekBar_barlevel, mMax);
        a.recycle();

        int dpi = getResources().getDisplayMetrics().densityDpi;
        if (dpi == 320) {
            thumb_width = 55;
        }
        thumb_width = ivthumb.getWidth();
        final LinearLayout layout = (LinearLayout) findViewById(R.id.myprobarid);
        ViewTreeObserver vto = layout.getViewTreeObserver();
       // layout.setOnTouchListener();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                if (!hasMeasured) {
                    myseekbar_w = layout.getWidth();
                    myseekbar_h = layout.getHeight();
                    //获取到宽度和高度后，可用于计算
                    initMyseekbarBitmap();
                    hasMeasured = true;
                    setProgress(mProgress_first);
                }
                return true;
            }
        });
    }

    public void setFirstProgress(int progress) {
        mProgress_first = progress;
    }

    private void initMyseekbarBitmap() {
        Resources res = getResources();
        float sx = 0.0f;
        float sy = 0.0f;
        //获取缩放率
        Bitmap bg_tmp = null;
        bg_tmp = BitmapFactory.decodeResource(res, R.drawable.seek_bar2);  // 获取图形资源文件
        Bitmap bp_fillbar_tmp = null;
        bp_fillbar_tmp = BitmapFactory.decodeResource(res, R.drawable.seek_progress2);  // 获取图形资源文件
        Bitmap bp_thumb_tmp = null;
        bp_thumb_tmp = BitmapFactory.decodeResource(res, R.drawable.seek_thumb_normal);

        //Bitmap bg_tmp = BitmapFactory.decodeResource(res, R.drawable.ver_seekbar_back2); 


        //Bitmap bp_fillbar_tmp = BitmapFactory.decodeResource(res, R.drawable.ver_seekbar_back_d3);
        //Bitmap bp_thumb_tmp = BitmapFactory.decodeResource(res, R.drawable.seek_thumb_normal);  // 获取图形资源文件
        sx = (float) myseekbar_w / (float) bg_tmp.getWidth();
        sy = (float) myseekbar_h / (float) bg_tmp.getHeight();

        Log.e(TAG, "myseekbar_w = " + myseekbar_w + "myseekbar_h" + myseekbar_h);

        int fillbar_w = bp_fillbar_tmp.getWidth();
        int fillbar_h = bp_fillbar_tmp.getHeight();

        Log.e(TAG, "fillbar_w = " + fillbar_w + "fillbar_h" + fillbar_h);
        // 定义矩阵对象  
        Matrix matrix_fillbar = new Matrix();
        matrix_fillbar.postScale(sx, sy);
        //bp.getWidth(), bp.getHeight()分别表示原始图片宽高  
        bp_fillbar = Bitmap.createBitmap(bp_fillbar_tmp, 0, 0, fillbar_w, fillbar_h, matrix_fillbar, true);


        int thumbbar_w = bp_thumb_tmp.getWidth();
        int thumbbar_h = bp_thumb_tmp.getHeight();

        Log.e(TAG, "thumbbar_w = " + thumbbar_w + "thumbbar_h" + thumbbar_h);
        // 定义矩阵对象  
        Matrix matrix_thumb = new Matrix();
        matrix_thumb.postScale(sx, sy);
        bp_thumb = Bitmap.createBitmap(bp_thumb_tmp, 0, 0, thumbbar_w, thumbbar_h, matrix_thumb, true);
    }

    public interface OnMyseekBarChangeListener {
        void onProgressChanged(MySeekBar seekBar, int progress);

        void onStartTrackingTouch(MySeekBar seekBar);

        void onStopTrackingTouch(MySeekBar seekBar);
    }

    public void setOnMyseekBarChangeListener(OnMyseekBarChangeListener l) {
        mOnMyseekBarChangeListener = l;
    }


    void onProgressRefresh() {
        if (mOnMyseekBarChangeListener != null) {
            mOnMyseekBarChangeListener.onProgressChanged(this, getProgress());
        }
    }

    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.e(TAG, "eventy"+event.getY()  + "event"+event.getX() );
                trackTouchEvent(event);
                invalidate();
                break;

            case MotionEvent.ACTION_MOVE:
                break;

            case MotionEvent.ACTION_UP:
                invalidate();
                break;

            case MotionEvent.ACTION_CANCEL:
                invalidate(); // see above explanation
                break;
        }
        super.onTouchEvent(event);
        return true;
    }

    public synchronized int getMax() {
        return mMax;
    }

    public synchronized int getProgress() {
        return mProgress;
    }

    private void trackTouchEvent(MotionEvent event) {
        final int thumbWidth = bp_thumb.getWidth();
        final int available = getWidth() - thumbWidth;
        int x = (int) event.getX();

        float step_dp = (float) available / (float) mMax;
        int progress = 0;
        float scale = 0;
        if (x < thumbWidth / 2) {
            progress = 0;
        } else if (x > available + thumbWidth / 2) {
            progress = mMax;
        } else {
            scale = (float) (x - thumbWidth / 2) / step_dp;
            progress = (int) scale;
            if (scale - (float) progress > 0.5) {
                progress++;
            }
        }

        setProgress((int) progress);
    }

    synchronized void setProgress(int progress) {
        if (progress < 0) {
            progress = 0;
        }

        if (progress > mMax) {
            progress = mMax;
        }

        if (progress != mProgress) {
            mProgress = progress;
            refreshProgress(R.id.pro_img, mProgress);
        }
    }

    private void refreshProgress(int proImg, int mProgress) {
        final int height = getHeight();
        final int thumbWidth = ivthumb.getWidth();
        final int available = getWidth() - thumbWidth;
        proImg_w = mProgress * available / mMax;
        if (proImg_w>0){
            proImg_w = mProgress * available / mMax+thumbWidth/2;
        }
        Log.e(TAG, "refreshProgressproImg_w = " + proImg_w);
        proImg_w = Math.max(proImg_w, 0);
        proImg_w = Math.min(proImg_w, available);
        Log.e(TAG, "refreshProgress proImg_w = " + proImg_w + "thumbbar_h=" + height);

        LayoutParams params = new LayoutParams(proImg_w, height);
        pro_img.setLayoutParams(params);
        pro_img.setScaleType(ScaleType.MATRIX);
        pro_img.setImageBitmap(bp_fillbar);

        Log.e(TAG, "refreshProgress thumbWidth = " + thumbWidth + "thumbbar_h" + height);

        LayoutParams params_thumb = new LayoutParams(thumbWidth, height);
        ivthumb.setLayoutParams(params_thumb);
        ivthumb.setImageBitmap(bp_thumb);

        onProgressRefresh();
    }
}


